package com.iqiyi.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iqiyi.domain.RecommendType;
import com.iqiyi.service.RecommendTypeService;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auther Z_zjl
 * @date 2022/12/18 20:22
 * @Description
 */
@RestController
@CrossOrigin
@RequestMapping("recommendType")
public class RecommendTypeController {
    @Resource
    private RecommendTypeService recommendTypeService;

    /**
     * 查询所有推荐方式
     * @return
     */
    @GetMapping("/findAll")
    public List<RecommendType> findAll(){
        List<RecommendType> list = recommendTypeService.list();
        return list;
    }

    /**
     * 查询当前的推荐方式，即表中isrecommend字段为1的记录
     * @return
     */
    @GetMapping("/nowRecommend")
    public List<RecommendType> nowRecommend(){
        // 查询当前的推荐方式
        List<RecommendType> list = recommendTypeService.list(Wrappers.lambdaQuery(RecommendType.class).eq(RecommendType::getIsrecommend, 1));
        return list;
    }

    /**更新推荐方式**/
    @PostMapping("/updateRecommend")
    @ApiImplicitParam(name = "ids", value = "ids", allowMultiple = true, dataTypeClass = List.class, paramType = "query")
    public String updateRecommend(@RequestBody List<Integer> ids){
        //TODO 1. 先取消之前所有的推荐方式,即 把isrecommend字段全部设置为0
        List<RecommendType> list = recommendTypeService.list();
        list.forEach(r -> {
            recommendTypeService.lambdaUpdate()
                    .set(RecommendType::getIsrecommend,0)
                    .eq(RecommendType::getId,r.getId())
                    .update(new RecommendType());
        });

        //TODO 2. 将传过来id对应的isrecommend字段改为1
        ids.forEach(id->{
            recommendTypeService.lambdaUpdate()
                    .set(RecommendType::getIsrecommend,1)
                    .eq(RecommendType::getId,id)
                    .update(new RecommendType());
        });
        return "推荐方式修改成功!";
    }
}
